{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 插入数据\n",
    "def save_model():\n",
    "    pass\n",
    "    # for data in supplier_list:\n",
    "    #     supplier = Supplier()\n",
    "    #     supplier.name = data['name']\n",
    "    #     supplier.address = data['address']\n",
    "    #     supplier.phone = data['phone']\n",
    "    #\n",
    "    #     supplier.save()\n",
    "\n",
    "    # for data in goods_list:\n",
    "    #     good = Goods(**data)\n",
    "    #     good.save()\n",
    "\n",
    "## 查询数据\n",
    "def query_model():\n",
    "    ## 获取一条数据\n",
    "    # good = Goods.get(Goods.id == 1)\n",
    "    # good = Goods.get_by_id(1)\n",
    "    # good = Goods[1]\n",
    "    # print(good.name)\n",
    "\n",
    "    ## 获取所有的数据\n",
    "    ## select 返回的是ModelSelect对象\n",
    "    ## select 参数是返回的字段\n",
    "    ## 循环的时候调用生成器获取数据\n",
    "    # goods = Goods.select(Goods.name, Goods.price)\n",
    "    # for good in goods:\n",
    "    #     print(good.name, good.price)\n",
    "\n",
    "    ## select * from goods where price > 100\n",
    "    # goods = Goods.select().where(Goods.price > 100)\n",
    "\n",
    "    ## select * from goods where price > 100 and click_num > 200\n",
    "    # goods = Goods.select().where((Goods.price > 100) & (Goods.click_num > 200))\n",
    "\n",
    "    ## select * from goods where name like \"%飞天\"\n",
    "    # goods = Goods.select().where(Goods.name.contains('飞天'))\n",
    "\n",
    "    ## good.id in (1,3)\n",
    "    # goods = Goods.select().where(Goods.id << [1, 3])\n",
    "    # goods = Goods.select().where((Goods.id==1) | (Goods.id==3))\n",
    "    # goods = Goods.select().where((Goods.id.in_([1, 3])))\n",
    "\n",
    "    ## select * from goods where price>click_num\n",
    "    # goods = Goods.select().where(Goods.price>Goods.click_num)\n",
    "\n",
    "    ## 排序\n",
    "    ## select * from goods order by price desc\n",
    "    # goods = Goods.select().order_by(Goods.price.desc())\n",
    "    # goods = Goods.select().order_by(-Goods.price)\n",
    "\n",
    "    ## 分页 42513\n",
    "    ## paginate(2, 3) 第二页取三条\n",
    "    goods = Goods.select().order_by(Goods.price).paginate(2, 3)\n",
    "\n",
    "    for good in goods:\n",
    "        print(good.id, good.name, good.price, good.click_num)\n",
    "\n",
    "## 更新数据\n",
    "def update_model():\n",
    "    ## update click_num=click_num+1 where id=1\n",
    "    # good = Goods.get_by_id(1)\n",
    "    # good.click_num += 1\n",
    "    # good.save()\n",
    "\n",
    "    ## update click_num=100 where id=1\n",
    "    ## 以下返回peewee.ModelUpdate 对象，需要执行execute()方法执行\n",
    "    # Goods.update(click_num=100).where(Goods.id == 1).execute()\n",
    "\n",
    "    ## update click_num=click_num+1 where id=1\n",
    "    Goods.update(click_num=Goods.click_num+1).where(Goods.id == 1).execute()\n",
    "\n",
    "## 删除数据\n",
    "def delete_model():\n",
    "    ## delete from goods where id=1\n",
    "    # good = Goods.get_by_id(1)\n",
    "    # good.delete_instance()\n",
    "\n",
    "    ## delete from goods where price>150\n",
    "    Goods.delete().where(Goods.price > 150).execute()\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    # save_model()\n",
    "    # query_model()\n",
    "    # update_model()\n",
    "    # delete_model()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
